#pragma once

#include <arpa/inet.h>
#include <cmath>
#include <iostream>
#include <map>
#include <mutex>
#include <netinet/in.h>
#include <thread>
#include <queue>
#include <string>
#include <sys/socket.h>
#include <unistd.h>
#include <vector>

#include "base64.h"
#include "CommandParameters.h"
#include "log.h"
#include "ParamProcessing.h"

class ServerSocket
{
public:
    ServerSocket(int port);
    ~ServerSocket();

    bool start();                           // 启动服务器
    void stop();                            // 停止服务器
    bool acceptClient();                    // 接受客户端连接
    bool sendData(const std::string &data); // 向客户端发送数据
    void receiveData();                     // 从客户端接收数据
    Message readData();                     // 读取队列数据

    bool getStopThreads();
    int &getClientSocket();

public:
    int m_clientSocket;

private:
    int m_port;
    int m_serverSocket;
    // int m_clientSocket;
    std::vector<uint8_t> m_receivedDataBuffer;
    std::queue<Message> m_messages;

    std::mutex m_mutex; // 互斥锁，保护共享资源
    // 用于线程的停止标志
    bool m_stopThreads = false;
    // 接收数据的线程
    std::thread m_receiveThread;
};

void UpperComputerProcessing(ServerSocket &UpperComputer);